import 'package:flutter/material.dart';
import 'package:intl/intl.dart' show DateFormat;
import 'package:intl/date_symbol_data_local.dart';
class Utils {
  static final double MILLIS_LIMIT = 1000.0;

  static final double SECONDS_LIMIT = 60 * MILLIS_LIMIT;

  static final double MINUTES_LIMIT = 60 * SECONDS_LIMIT;

  static final double HOURS_LIMIT = 24 * MINUTES_LIMIT;

  static final double DAYS_LIMIT = 30 * HOURS_LIMIT;

  static Locale curLocale;

  static String getImagePath(String name, {String format: 'png'}) {
//    return 'assets/images/$name.$format';
    return 'assets/$name';
  }

  static String getDateStr(DateTime date) {
    if (date == null || date.toString() == null) {
      return "";
    } else if (date.toString().length < 10) {
      return date.toString();
    }
    return date.toString().substring(0, 10);
  }

  static String getDateFromMill(int mill) {
    initializeDateFormatting();
    DateTime date = new DateTime.fromMillisecondsSinceEpoch(mill, isUtc: true);
    String txt = DateFormat('mm:ss:SS', 'en_GB').format(date);
    return txt.substring(0, 8);
  }
  /// 时间转字符串
  ///
  static String duration2String(Duration duration) {
    return duration?.toString()?.split('.')?.first ?? '0:00:00';
  }

//  static String getDateFromMill({@required int mill}) {
//    DateTime recordedDate = DateTime.fromMillisecondsSinceEpoch(mill);
////    int hour = recordedDate.hour;
//    int minutes = recordedDate.minute;
//    int second = recordedDate.second;
//    int ss = recordedDate.millisecond;
//
//    return '$minutes:$second';
//  }

//  static String parseDate(DateTime dateTime) {
//    if (dateTime != null) {
//      return formatDate(dateTime, [yyyy, '-', mm, '-', dd, ' ']);
//    } else {
//      return '';
//    }
//  }

//  static String changeDateStr(String dateTime) {
//    return formatDate(DateTime.parse(dateTime), [yyyy, '-', mm, '-', dd]);
//  }

  ///日期格式转换
//  static String getNewsTimeStr(DateTime date) {
//    int subTimes =
//        DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
//
//    if (subTimes < MILLIS_LIMIT) {
//      return (curLocale != null)
//          ? (curLocale.languageCode != "zh") ? "right now" : "刚刚"
//          : "刚刚";
//    } else if (subTimes < SECONDS_LIMIT) {
//      return (subTimes / MILLIS_LIMIT).round().toString() +
//          ((curLocale != null)
//              ? (curLocale.languageCode != "zh") ? " seconds ago" : " 秒前"
//              : " 秒前");
//    } else if (subTimes < MINUTES_LIMIT) {
//      return (subTimes / SECONDS_LIMIT).round().toString() +
//          ((curLocale != null)
//              ? (curLocale.languageCode != "zh") ? " min ago" : " 分钟前"
//              : " 分钟前");
//    } else if (subTimes < HOURS_LIMIT) {
//      return (subTimes / MINUTES_LIMIT).round().toString() +
//          ((curLocale != null)
//              ? (curLocale.languageCode != "zh") ? " hours ago" : " 小时前"
//              : " 小时前");
//    } else if (subTimes < DAYS_LIMIT) {
//      return (subTimes / HOURS_LIMIT).round().toString() +
//          ((curLocale != null)
//              ? (curLocale.languageCode != "zh") ? " days ago" : " 天前"
//              : " 天前");
//    } else {
//      return getDateStr(date);
//    }
//  }

//  static getLocalPath() async {
//    Directory appDir;
//    if (Platform.isIOS) {
//      appDir = await getApplicationDocumentsDirectory();
//    } else {
//      appDir = await getExternalStorageDirectory();
//    }
//
//    var status = await Permission.storage.status;
//    if (status != PermissionStatus.granted) {
//      Map<Permission, PermissionStatus> statuses = await [
//        Permission.storage,
//      ].request();
//      if (statuses[Permission.storage] != PermissionStatus.granted) {
//        return null;
//      }
//    }
//    String appDocPath = appDir.path + "/gsygithubappflutter";
//    Directory appPath = Directory(appDocPath);
//    await appPath.create(recursive: true);
//    return appPath;
//  }

  static String removeTextTag(String description) {
    if (description != null) {
      String reg = "<g-emoji.*?>.+?</g-emoji>";
      RegExp tag = new RegExp(reg);
      Iterable<Match> tags = tag.allMatches(description);
      for (Match m in tags) {
        String match = m
            .group(0)
            .replaceAll(new RegExp("<g-emoji.*?>"), "")
            .replaceAll(new RegExp("</g-emoji>"), "");
        description = description.replaceAll(new RegExp(m.group(0)), match);
      }
    }
    return description;
  }

  static splitFileNameByPath(String path) {
    return path.substring(path.lastIndexOf("/"));
  }

  ///获取设备信息
//  static Future<String> getDeviceInfo() async {
//    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
//    if (Platform.isAndroid) {
//      return "";
//    }
//    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
//    return iosInfo.model;
//  }

//  static launchUrl(context, String url) {
//    if (url == null && url.length == 0) return;
//    Uri parseUrl = Uri.parse(url);
//    bool isImage = isImageEnd(parseUrl.toString());
//    if (parseUrl.toString().endsWith("?raw=true")) {
//      isImage = isImageEnd(parseUrl.toString().replaceAll("?raw=true", ""));
//    }
//    if (isImage) {
//      NavigatorUtils.gotoPhotoViewPage(context, url);
//      return;
//    }
//
//    if (parseUrl != null &&
//        parseUrl.host == "github.com" &&
//        parseUrl.path.length > 0) {
//      List<String> pathnames = parseUrl.path.split("/");
//      if (pathnames.length == 2) {
//        //解析人
//        String userName = pathnames[1];
//        NavigatorUtils.goPerson(context, userName);
//      } else if (pathnames.length >= 3) {
//        String userName = pathnames[1];
//        String repoName = pathnames[2];
//        //解析仓库
//        if (pathnames.length == 3) {
//          NavigatorUtils.goReposDetail(context, userName, repoName);
//        } else {
//          launchWebView(context, "", url);
//        }
//      }
//    } else if (url != null && url.startsWith("http")) {
//      launchWebView(context, "", url);
//    }
//  }

  static bool isShow = false;

//  static Future<Null> showLoadingDialog(BuildContext context,
//      {String message = '努力加载中...'}) {
//    return NavigatorUtils.showQZDialog(
//        context: context,
//        builder: (BuildContext context) {
//          return new Material(
//              color: Colors.transparent,
//              child: WillPopScope(
//                onWillPop: () => new Future.value(false),
//                child: Center(
//                  child: new Container(
//                    width: 200.0,
//                    height: 200.0,
//                    padding: new EdgeInsets.all(4.0),
//                    decoration: new BoxDecoration(
//                      color: Colors.transparent,
//                      //用一个BoxDecoration装饰器提供背景图片
//                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
//                    ),
//                    child: new Column(
//                      mainAxisAlignment: MainAxisAlignment.center,
//                      children: <Widget>[
//                        new Container(child: SpinKitWave(color: Colors.white)),
//                        new Container(height: 10.0),
//                        new Container(
//                            child: new Text(message,
//                                style: TextStyle(
//                                    color: Colors.white, fontSize: 16))),
//                      ],
//                    ),
//                  ),
//                ),
//              ));
//        });
//  }
}
